/*
 * 弹窗事件处理
 */
var submitClick = function(opts){
	$("#"+opts.submitBtn+"").click(function(){
		var defaultValue = "";
		var defaultName = "";
		var multiple = $("#"+opts.multiple+"");
		var relation = opts.relation;
		if(relation != undefined){
			var options = "";
			multiple.find("option").each(function(i,o){
				defaultValue += o.value+",";
				defaultName += o.text+",";
				options += $(o)[0].outerHTML;
			});
			var _relation = parent.$("#"+relation+"");
			if(_relation.get(0) != undefined){
				_relation.find("option").remove();
				_relation.html(options);
			}else{
				var mainFrame = top.$("#mainFrame");
				var viewFrame = mainFrame.contents().find("#viewFrame");
				if (viewFrame.attr("id") == undefined) {
					_relation = mainFrame.contents().find("#"+relation+"");
					_relation.find("option").remove();
					_relation.html(options);
				}else{
					_relation = viewFrame.contents().find("#"+relation+"");
					_relation.find("option").remove();
					_relation.html(options);
				}
			}
		}else{
			multiple.find("option").each(function(key,item){
				defaultValue += item.value+",";
				defaultName += item.text+",";
			});
		}
		var len1 = defaultValue.lastIndexOf(",");
		var len2 = defaultName.lastIndexOf(",");
		if(relation != undefined){
			if(parent.$("#"+relation+"").get(0) != undefined){
				parent.$("#"+opts.code+"").attr("value",defaultValue.substring(defaultValue,len1));
				parent.$("#"+opts.name+"").attr("value",defaultName.substring(defaultName,len2));
			}else{
				var mainFrame = top.$("#mainFrame");
				var viewFrame = mainFrame.contents().find("#viewFrame");
				if (viewFrame.attr("id") == undefined) {
					mainFrame.contents().find("#"+opts.code+"").attr("value",defaultValue.substring(defaultValue,len1));
					mainFrame.contents().find("#"+opts.name+"").attr("value",defaultName.substring(defaultName,len2));
				}else{
					viewFrame.contents().find("#"+opts.code+"").attr("value",defaultValue.substring(defaultValue,len1));
					viewFrame.contents().find("#"+opts.name+"").attr("value",defaultName.substring(defaultName,len2));
				}
			}
		}else{
			var mainFrame = top.$("#mainFrame");
			var viewFrame = mainFrame.contents().find("#viewFrame");
			if (viewFrame.attr("id") == undefined) {
				mainFrame.contents().find("#"+opts.code+"").attr("value",defaultValue.substring(defaultValue,len1));
				mainFrame.contents().find("#"+opts.name+"").attr("value",defaultName.substring(defaultName,len2));
			}else{
				viewFrame.contents().find("#"+opts.code+"").attr("value",defaultValue.substring(defaultValue,len1));
				viewFrame.contents().find("#"+opts.name+"").attr("value",defaultName.substring(defaultName,len2));
			}
		}
		window.parent.ymPrompt.doHandler('',true);
	});
}

var syncClick = function(opts){
	$("#"+opts.submitBtn+"").click(function(){
		var node = $("#"+opts.node+"").val();
		var syncUrl = opts.submitUrl+"&id="+node;
		$.getJSON(syncUrl,function(rs){
			alert(rs)
		});
	});
}

var removeClick = function(opts){
	$("#"+opts.multiple+"").dblclick(function(){
		$("#"+opts.multiple+" option[value='"+this.value+"']").remove();  
	});
	$("#"+opts.removeBtn+"").click(function(){
		var checkValue = $("#"+opts.multiple+" option:selected").val();
		$("#"+opts.multiple+" option[value='"+checkValue+"']").remove(); 
	});
};

var closeClick = function(opts) {
	$("#" + opts.closeBtn + "").click(function() {
		window.parent.ymPrompt.doHandler('', true);
	});
};
var bindUserClick = function(opts) {
	$("#" + opts.btnBind + "").click(function() {
		window.parent.ymPrompt.doHandler('', true);
		var mainFrame = top.$("#mainFrame");
		var viewFrame = mainFrame.contents().find("#viewFrame");
		if (viewFrame.attr("id") == undefined) {
			mainFrame.contents().find("#"+opts.code+"").val($("#node").val());
			mainFrame.contents().find("#"+opts.name+"").val($("#nodeText").val());
		}else{
			viewFrame.contents().find("#"+opts.code+"").val($("#node").val());
			viewFrame.contents().find("#"+opts.name+"").val($("#nodeText").val());
		}
	});
};
var bindTravelAgencyClick = function(opts) {
	$("#" + opts.btnBind + "").click(function() {
		window.parent.ymPrompt.doHandler('', true);
		var mainFrame = top.$("#mainFrame");
		var viewFrame = mainFrame.contents().find("#viewFrame");
		if (viewFrame.attr("id") == undefined) {
			mainFrame.contents().find("#"+opts.code+"").val($("#node").val());
			mainFrame.contents().find("#"+opts.name+"").val($("#nodeText").val());
		}else{
			viewFrame.contents().find("#"+opts.code+"").val($("#node").val());
			viewFrame.contents().find("#"+opts.name+"").val($("#nodeText").val());
		}
	});
};
var loadMultiple = function(opts){
	var options = "";
	var relation = opts.relation;
	if(relation != undefined){
		var _relation = parent.$("#"+relation+"");
		if(_relation.get(0) == undefined){
			var mainFrame = top.$("#mainFrame");
			var viewFrame = mainFrame.contents().find("#viewFrame");
			if (viewFrame.attr("id") == undefined) {
				_relation = mainFrame.contents().find("#"+relation+"");
			}else{
				_relation = viewFrame.contents().find("#"+relation+"");
			}
		}
		_relation.find("option").each(function(i,o){
			options += $(o)[0].outerHTML;
		});
		var multiple = $("#"+opts.multiple+"");
		multiple.find("option").remove();
		multiple.html(options);
	}else{
		var defaultCode = "";
		var defaultName = "";
		var mainFrame = top.$("#mainFrame");
		var viewFrame = mainFrame.contents().find("#viewFrame");
		if (viewFrame.attr("id") == undefined) {
			defaultCode = mainFrame.contents().find("#"+opts.code+"").val();
			defaultName = mainFrame.contents().find("#"+opts.name+"").val();
		}else{
			defaultCode = viewFrame.contents().find("#"+opts.code+"").val();
			defaultName = viewFrame.contents().find("#"+opts.name+"").val();
		}
		if(defaultCode != ""){
			var multiple = $("#"+opts.multiple+"");
			var arrsCode = defaultCode.split(",");
			var arrsName = defaultName.split(",");
			for(var i=0;i<arrsCode.length;i++){
				multiple.get(0).options.add(new Option(""+arrsName[i]+"", ""+arrsCode[i]+""));
			}
		}
	}
};